package com.czk.model.ptotorype.practice.serializable;

import java.io.IOException;
import java.io.Serializable;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/10/21 11:33
 */
public class Person implements Serializable {
    private static final long serialVersionUID = 15445454564656L;
    private String name;
    /**
     * transient不能被序列化
     */
    private transient Integer age;

    /**
     * 将不能反序列化到文件的字段写入到流
     *
     * @param out
     * @throws IOException
     */
    private void writeObject(java.io.ObjectOutputStream out)
            throws IOException {
        System.out.println("实现序列化了");
        out.defaultWriteObject();
        out.writeObject(this);
        // 将不能序列化的年龄写入
        out.writeInt(age);
    }

    /**
     * 读取不能反序列化的流
     * 参考 HashSet的实现
     *
     * @param in
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void readObject(java.io.ObjectInputStream in)
            throws IOException, ClassNotFoundException {
        System.out.println("实现反序列化了");
        in.defaultReadObject();
        in.readObject();
        age = in.readInt();
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
